<?php
class IPC
{
	private $_sock  = null;
	
	final public function __construct()
	{
		$this->_make_connection() || exit;
	}
	
	private function _make_connection()
	{
		$this->_sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
		
		return socket_connect($this->_sock, '/tmp/mt.sock');
	}
	
	final public function spawn($callback, $params = array())
	{
		if(($pid = pcntl_fork()) == 0)
		{
			$this->_make_connection();
			
			call_user_func_array($callback, $params);
			
			exit;
		}
		
		return $pid;
	}
	
	public function __get($name)
	{
		$data = '';

		socket_write($this->_sock, $name."\x00");
		
		while($packet = socket_read($this->_sock, 4096))
		{
			$data .= $packet;
			
			if(substr($packet, -1) == "\x00")
			{
				$data = substr($data, 0, -1);
				
				break;
			}
		}

		return unserialize($data);
	}
	
	public function __set($name, $value)
	{
		socket_write($this->_sock, "\x01".serialize(array($name, $value))."\x00");
	}
}